package com.leicx.guli.ware.config;

import com.leicx.guli.common.constant.WareMqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * @author daxiong
 * @date 2022/8/16 上午11:38
 */
@SpringBootConfiguration
public class MyWareMqConfig {

    @Bean
    public Exchange stockEventExchange() {
        // String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(WareMqConstant.EXCHANGE_STOCK_EVENT, true, false);
    }

    @Bean
    public Queue stockReleaseStockQueue() {
        // String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue(WareMqConstant.QUEUE_STOCK_RELEASE_STOCK, true, false, false, null);
    }

    @Bean
    public Queue stockDelayQueue() {
        // String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", WareMqConstant.EXCHANGE_STOCK_EVENT);
        args.put("x-dead-letter-routing-key", WareMqConstant.ROUTE_KEY_STOCK_RELEASE);
        args.put("x-message-ttl", 180000);
        return new Queue(WareMqConstant.QUEUE_STOCK_DELAY, true, false, false, args);
    }

    @Bean
    public Binding stockReleaseBinding() {
        // String destination, DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        return new Binding(WareMqConstant.QUEUE_STOCK_RELEASE_STOCK, Binding.DestinationType.QUEUE, WareMqConstant.EXCHANGE_STOCK_EVENT,
                "stock.release.#", null);
    }

    @Bean
    public Binding stockLockedBinding() {
        // String destination, DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        return new Binding(WareMqConstant.QUEUE_STOCK_DELAY, Binding.DestinationType.QUEUE, WareMqConstant.EXCHANGE_STOCK_EVENT,
                WareMqConstant.ROUTE_KEY_STOCK_LOCKED, null);
    }
}
